package com.mjfeed.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.mjfeed.fragment.FragmentIndicator;
import com.mjfeed.fragment.FragmentIndicator.OnIndicateListener;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

/**
 * 主界面
 * 
 * @author zhaominglei
 * @date 2014-12-2
 * 
 */
public class MainActivity extends FragmentActivity {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	
	private static Fragment[] fragments;
	private boolean isExit = false;
	private TimerTask timerTask;
	private String referer; //来自那个链接
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
    	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//底部指示器
		referer = getIntent().getStringExtra("referer");
		if (referer != null && !referer.equals("")) {
			if (referer.equals("ShoppingFragment") || referer.equals("ProductDetailActivity")
					|| referer.equals("CheckoutActivity") || referer.equals("PayActivity")) {
				setFragmentIndicator(2);
			} else if (referer.equals("RegisterLoginActivity")) {
				setFragmentIndicator(3);
			} else {
				setFragmentIndicator(0);
			}
		} else {
			setFragmentIndicator(0);
		}
	}

	public void setFragmentIndicator(final int whichIsDefault) {
		fragments = new Fragment[4];
		fragments[0] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_home);
		fragments[1] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_service);
		fragments[2] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_shopping);
		fragments[3] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_member);
		getSupportFragmentManager().beginTransaction().hide(fragments[0])
				.hide(fragments[1]).hide(fragments[2]).hide(fragments[3])
				.show(fragments[whichIsDefault]).commit();

		FragmentIndicator indicator = (FragmentIndicator) findViewById(R.id.indicator);
		FragmentIndicator.setIndicator(whichIsDefault);
		indicator.setOnIndicateListener(new OnIndicateListener() {
			@Override
			public void onIndicate(View v, int which) {
				getSupportFragmentManager().beginTransaction()
						.hide(fragments[0]).hide(fragments[1])
						.hide(fragments[2]).hide(fragments[3])
						.show(fragments[which]).commit();
			}
		});

	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
}
